<?php
class controller_note extends controller_base {

	/**
	 * 列表
	 */
	public function action_index() {
		// 指定ID, 支持多个
		if(!empty($_GET['id'])) {
			preg_match_all('/\d+/', $_GET['id'], $matches);
			$ids = isset($matches[0]) ? $matches[0] : array();
			$where = 'not_id IN ('.implode(',', $ids).')';
		// 指定天数, 支持段
		} elseif (!empty($_GET['day'])) {
			$days = explode(',', $_GET['day']);
			$start_day = isset($days[0]) ? intval($days[0]) : 1;
			$limit_day = isset($days[1]) ? intval($days[1]) : 0;

			$start_time = strtotime(date('Y-m-d')) - ($start_day - 1) * 86400;
			$end_time = $limit_day ? $start_time + $limit_day * 86400 : 0;

			$where = 'create_time >= '.$start_time;
			$end_time > 0 && $where .= ' AND create_time < '.$end_time;
		// 指定日期，支持段
		} elseif (!empty($_GET['date'])) {
			$dates = explode(',', $_GET['date']);	
			$start_time = isset($dates[0]) ? strtotime($dates[0]) : strtotime(date('Y-m-d'));	
			$end_time = isset($dates[1]) ? strtotime($dates[1]) + 86400 : $start_time + 86400;

			$where = 'create_time >= '.$start_time.' AND create_time < '.$end_time;
		// 查看全部
		} elseif (isset($_GET['all'])) {
			$where = '1';
		// 默认
		} else {
			$where = 'create_time >= '.(strtotime(date('Y-m-d')) - 29 * 86400);
		}

		// 关键字搜索
		if(isset($_GET['key'])) {
			$key = trim(addslashes($_GET['key']));
			$where .= " AND content LIKE '%$key%'";
		}

		$model_note = new model_note();

		$this->assign('notes', $model_note->daily_notes($where));	// 每日数据
		$this->assign('count_query', $model_note->get_query_count());	// 每日数据
		$this->assign('count_all', $model_note->count());	// 总数
	}
	
	/**
	 * 表单
	 */
	public function action_form() {
		if(empty($_GET['not_id'])) {
			//添加表单
			$res['action'] = 'add';
			$res['not_id'] = '';
			$res['content'] = '';
			$res['rank'] = '1';
			$res['create_time'] = time();
			$res['end_time'] = $res['create_time'];
		} else {
			//修改表单
			$not_id = $_GET['not_id'];
			$model_note = new model_note();
			$res = $model_note->find("not_id='$not_id'");
			$res['action'] = 'edit';
		}
		
		//assign
		$this->assign('res', $res);
	}
	
	/**
	 * 添加
	 */
	public function action_add() {
		$model_note = new model_note();
		$data = array();
		$data['content'] = empty($_POST['content']) ? 'None' : $_POST['content'];
		$data['rank'] = empty($_POST['rank']) ? 0 : $_POST['rank'];
		$data['create_time'] = empty($_POST['create_time']) ? time() : library_common::str2date($_POST['create_time'], 1);
		$data['end_time'] = empty($_POST['end_time']) ? 0 : time() + $_POST['end_time']*3600;
		//设置了end_time默认更改rank为2
		empty($_POST['end_time']) == false && $data['rank'] = 2;
		
		$model_note->create($data);
		
		//log
		new model_logs('note add - '.$data['content']);
			
		$this->message('添加成功', '?m=note');
	}
	
	/**
	 * ajax 自动保存模式的添加功能
	 *
	 */
	public function action_form_ajax() {
		if(!empty($_GET['not_id'])) {
			$model_note = new model_note();
			$res = $model_note->find("not_id = {$_GET['not_id']}");

			//log
			new model_logs('note edit ajax - '.print_r($res, 1));
		
			$this->assign('res', $res);
		} else {
			// 添加
		}
	}
	
	/**
	 * ajax 添加
	 */
	public function action_add_ajax() {
		$model_note = new model_note();
		
		$data['not_id'] = empty($_POST['id']) ? '' : $_POST['id'];
		$data['content'] = empty($_POST['cnt']) ? 'None' : $_POST['cnt'];
		
		if($data['not_id']) {
			$model_note->update($data);
			echo htmlspecialchars(mb_substr($data['content'], -12, 12, 'utf-8'));
		} else {
			$data['create_time'] = time();
			$data['rank'] = 1;
			$data['end_time'] = 0;
			
			$not_id = $model_note->create($data);
			new model_logs('note add_ajax - '.$not_id.' - '.$data['content']);
			echo $not_id;
		}
	}
	
	/**
	 * ajax 删除
	 */
	public function action_delete_ajax() {
		$model_note = new model_note();
		$not_id = $_GET['id'];
		
		$model_note->delete("not_id='$not_id'");
		
		//log
		new model_logs('note delete_ajax - '.$not_id);
	}
	
	/**
	 * 修改
	 */
	public function action_edit() {
		$model_note = new model_note();
		$data = array();
		$data['not_id'] = $_POST['not_id'];
		$data['content'] = $_POST['content'];
		$data['rank'] = $_POST['rank'];
		$data['create_time'] = library_common::str2date($_POST['create_time'], 1);
		
		$model_note->update($data);
		
		//log
		new model_logs('note edit - '.$data['content']);
		
		$this->message('修改成功', '?m=note');
	}
	
	/**
	 * 删除
	 */
	public function action_delete() {
		$model_note = new model_note();
		$not_id = $_GET['not_id'];
		
		$res = $model_note->find("not_id='$not_id'");
		$model_note->delete("not_id='$not_id'");
		
		//log
		new model_logs('note delete - '.json_encode($res));
		
		$this->message('删除成功', '?m=note');
	}
	
	/**
	 * rank设置
	 */
	public function action_rank() {
		$not_id = $_GET['not_id'];
		$rank = $_GET['rank'];
		
		$model_note = new model_note();
		$model_note->rank_set($not_id, $rank);
		
		//log
		new model_logs("rank set - not_id:$not_id / rank:$rank");
		
		$this->message('设置Rank成功', '?m=note&a=list');
	}
	
	/**
	 * set页
	 */
	public function action_set() {
		$not_id = $_GET['not_id'];
		
		$model_note = new model_note();
		$res = $model_note->find("not_id='$not_id'");
	
		//assign
		$this->assign('res', $res);
	}
	
	/**
	 * get 获取 返回JSON数据
	 */
	public function action_get() {
		$time = $_GET['time'];
		$time_start = strtotime(date('Y-m-d', $time));
		$time_end = strtotime(date('Y-m-d', $time)) + 86400 - 1;
		
		$model_note = new model_note();
		$res = $model_note->find_all("create_time >= $time_start and create_time <= $time_end");
		foreach ($res as &$row) {
			$row['content'] = '<b>'.date('H:i ', $row['create_time']).'</b>'.nl2br(htmlspecialchars($row['content']));
		}
		
		echo json_encode($res);
	}
}
?>
